/*------------------------------------------------------------------------------------------------------------------------------------------
--		SOURCE FILE:	Translate.cpp		-	Represents the "Presentation Layer" of the ISO model.  Builds frames from chars read in 
--												From the serial port, and extracts the data portion of the frame so that it can be passed
--												to the "Application Layer" for printing.
												
--
--		PROGRAM:		Wireless Modem Protocol Driver
--
--		FUNCTIONS:		int buildFrame(char charRead, PTTYINFO pinfo)
--						int getData(PTTYINFO pinfo)
--						void packetize(PTTYINFO pinfo)
--						char ToggleType(char type)
--
--		DATE:			October 24, 2011
--
--		REVISIONS:		1.0
--
--		DESIGNER:		Mike Zobac/Chris Sim
--
--		PROGRAMMER		Mike Zobac
--
--		NOTES:			All of these functions are passed a pointer to a TTYINFO struct.  This structure contains all of the variables which
--						would otherwise have to be global, or extern globals. 
--------------------------------------------------------------------------------------------------------------------------------------------*/
#include "Translate.h"
#include "Command.h"
#include "Connect.h"
#include "crc.h"


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		Build Frame
--
--		DATE:			Nov 1st, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		int buildFrame(char charRead)
--
--		RETURNS:		1 if data is still to come, and 0 if the last byte has been read.
--
--		NOTES:			Uses a CRC implementation Copyright (c) 2000 by Michael Barr.
--------------------------------------------------------------------------------------------------------------------------------------------*/

int buildFrame(char charRead, PTTYINFO pinfo)
{
    static int count = 0;

    if(count < FRAMESIZE-1)
    {
        pinfo->rxFrame[count] = charRead;
        count++;
        return 1;
    }
    else
    {
       pinfo->rxFrame[count] = charRead;
       count = 0;
	   SetEvent(pinfo->frameBuilt);
    }
}




/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		getData
--
--		DATE:			October 24, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		char* getData(char * sequence)
--
--		RETURNS:		Char* which cotains the data portion of the frame to be displayed.
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

int getData(PTTYINFO pinfo)
{
	size_t  i;

    if(pinfo->rxFrame[TYPEINDEX] != pinfo->frameTypeExpected)  // already processed this frame
        return 1;
    
    if(pinfo->rxFrame[EOTCHECK] != '\x03')   //  EOT.  Last frame to receive
        pinfo->receivedLastFrame = TRUE;
       
    for(i = STARTOFDATA; i < DATASIZE; i++)  // PROBABLY CAN USE MEMCOPY TO IMPROVE THIS!
    {
        pinfo->printData[i - 2] = pinfo->rxFrame[i];
    } 

	return 0;
}




/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:	packetize	
--
--		DATE:			
--
--		DESIGNER:   
--
--		PROGRAMMER:	Chris Sim	
--
--		INTERFACE:	void packetize(PTTYINFO pinfo)	
--
--		RETURNS:    TRUE if there is no error found, and FALSE if an error is found
--
--		NOTES:		Uses a CRC implementation Copyright (c) 2000 by Michael Barr.	
--------------------------------------------------------------------------------------------------------------------------------------------*/

void packetize(PTTYINFO pinfo) {

	size_t	size;
	unsigned short	tmp;


	//pinfo->txFrame = (char) malloc(sizeof(char) * FRAMESIZE);

	//Empty the buffer ( character array) ------ not sure if we need to implement or not

	//Read 1000B of data from file, 1 char at a time
	//Store the data in the buffer
	size = fread(pinfo->txFrame+2, sizeof(char), DATASIZE, pinfo->inputFile);
	//memcpy(&pinfo->txFrame+2, &pinfo->inputFile, 1000);

	//Append frame type char(1B) in front of the data	
	pinfo->txFrame[TYPEINDEX] = pinfo->frameTypeToWrite;

	//Append STX char in front of the frame type char
	pinfo->txFrame[0] = '\x02';

	if(size < DATASIZE) {
		//While reading chars, if EOF is detected, append EOT instead of ETX
		for(size_t i = STARTOFDATA + size; i < EOTCHECK; i++)
		{
			pinfo->txFrame[i] = '\x00';
		}
		pinfo->txFrame[EOTCHECK] = '\x04';
		pinfo->endoftransmission = true;
		ResetEvent(pinfo->FrameToSend);
	}
	else {
		//Append ETX char after the data if there are more to read
		pinfo->txFrame[EOTCHECK] = '\x03';
	}

	
	//Call CRC function and it computes 2 CRC chars and append after the frame
	
    tmp = crcSlow(pinfo->txFrame, FRAMESIZE);
    pinfo->txFrame[CRC2] = tmp;
	pinfo->txFrame[CRC1] = (tmp >> 8);
	pinfo->frameTypeToWrite = ToggleType(pinfo->frameTypeToWrite);
}


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		Toggle Type
--
--		DATE:			November 28, 2011
--
--		DESIGNER:		Chris Sim
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		char ToggleType(char type)
--
--		RETURNS:		The char the type has been set to
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

char ToggleType(char type)
{
    char key = '\x01';
	return (type ^ key);
}

